package com.cloud.school.bus.teacherhelper.base.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dialog.CustomWaitDialog;
import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.ClassInfo;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.entitys.Teacher;
import com.cloud.school.bus.teacherhelper.modules.init.InitActivity;
import com.cloud.school.bus.teacherhelper.modules.login.LoginActivity;
import com.cloud.school.bus.teacherhelper.modules.uploadlist.uploadutils.UploadFileUtils;
import com.umeng.analytics.MobclickAgent;

import android.R.string;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.PixelFormat;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;

/**
 * activity基类
 * 
 * @author lenovo
 * 
 */
abstract public class BaseActivity extends Activity {

	protected Context mContext;

	protected TeacherHelperApplication mApplication;

	private CustomWaitDialog mCustomWaitDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFormat(PixelFormat.RGBA_8888);

		mContext = this;

		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();

		DebugLog.setTag(mContext.getClass().getName());
	}

	protected void init() {

	}

	protected void setListener() {

	}

	protected void setTitleNavBar() {

	}

	/**
	 * 显示等待对话框
	 * 
	 * @param messageString
	 * @param onDismissListener
	 */
	protected void showWaitDialog(String messageString,
			final OnKeyCancel onKeyCancel) {
		if (null == mCustomWaitDialog) {
			mCustomWaitDialog = new CustomWaitDialog(mContext,
					com.android.support.jhf.R.style.CustomWaitDialog);
			mCustomWaitDialog.setCancelable(true);
			mCustomWaitDialog.setCanceledOnTouchOutside(false);
			mCustomWaitDialog.setMessage(messageString);
			mCustomWaitDialog.setOnKeyCancelListener(new OnKeyCancel() {

				@Override
				public void onKeyCancelListener() {
					if (null != onKeyCancel) {
						onKeyCancel.onKeyCancelListener();
					}
					if (null != mCustomWaitDialog) {
						mCustomWaitDialog.cancel();
						mCustomWaitDialog = null;
					}
				}
			});
			mCustomWaitDialog.show();
		}
	}

	protected void hideWaitDialog() {
		if (null != mCustomWaitDialog) {
			mCustomWaitDialog.cancel();
			mCustomWaitDialog = null;
		}
	}

	/**
	 * 判断网络连接监听
	 */
	private BroadcastReceiver mNetConnectBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent
					.getAction())) {
				UploadFileUtils.getUploadUtils().setContext(mContext);
				UploadFileUtils.getUploadUtils().uploadFileService();
			}
		}
	};
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		
		DebugLog.logI("Activity onSaveInstanceState");
//		HandlerToastUI.getHandlerToastUI(mContext.getApplicationContext(), "onSaveInstanceState");
		outState.putString("loginToken", NetworkClient.getNetworkClient().getLoginToken());
		outState.putSerializable("StudentList", (ArrayList<Student>) mApplication.mStudentList);
		outState.putSerializable("Teacher", mApplication.mTeacher);
		outState.putSerializable("ClassInfo", mApplication.mClassInfo);
		outState.putSerializable("LoginSetting", mApplication.mLoginSetting);
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		
		super.onRestoreInstanceState(savedInstanceState);
		if(null != savedInstanceState){
			DebugLog.logI("Activity onRestoreInstanceState");
	//		HandlerToastUI.getHandlerToastUI(mContext.getApplicationContext(), "onRestoreInstanceState");
			NetworkClient.getNetworkClient().setLoginToken(savedInstanceState.getString("loginToken"));
			mApplication.mStudentList = (List<Student>) savedInstanceState.getSerializable("StudentList");
			mApplication.mTeacher = (Teacher) savedInstanceState.getSerializable("Teacher");
			mApplication.mClassInfo = (ClassInfo) savedInstanceState.getSerializable("ClassInfo");
			mApplication.mLoginSetting = (LoginSetting) savedInstanceState.getSerializable("LoginSetting");
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		// 注册广播
		if (!(this instanceof LoginActivity) && !(this instanceof InitActivity)) {
			IntentFilter netConnectIntentFilter = new IntentFilter(
					ConnectivityManager.CONNECTIVITY_ACTION);
			mContext.registerReceiver(mNetConnectBroadcastReceiver,
					netConnectIntentFilter);
		}else {
//			DebugLog.logI("onResume LoginActivity InitActivity");
		}
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		if (!(this instanceof LoginActivity) && !(this instanceof InitActivity)) {
			mContext.unregisterReceiver(mNetConnectBroadcastReceiver);
		}else {
//			DebugLog.logI("onPause LoginActivity InitActivity");
		}

		MobclickAgent.onPause(this);
	}

}
